<?php 
require ("inc/common.inc.php");
ob_start();
require ("user/checkLogin.php");
require ("inc/cart.php");
$cart = new Cart();
if ($actions=="del"){
    $rowid = $id;

	if (!$rowid){
		$fun->popmassage("请先选择要删除的商品!","","popback");
		exit();
	}
	$cart->delete($rowid);
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=gouwuche.php\">";
	exit();
	
}elseif ($actions=="settle"){
	if(empty($_COOKIE['userid']) || $_COOKIE['userid']=="" || empty($_COOKIE['username']) || $_COOKIE['username']==""){
		$result['error'] = "您需要登录后才可以操作。";
		echo json_encode($result);
		exit();
	}else{
		$mxuserid = $_COOKIE['userid'];
		$mxusername = $_COOKIE['username'];
		$mxnickname = $_COOKIE['nickname'];
	
	}
	$content = $cart->contents();
	$allprice = $content['cart_total'];
	
	$mxuserInfo = $db->getinfo("select * from mx_app_user where id=$mxuserid");
	if($allprice > $mxuserInfo['money']){
		$return["success"]=false;
		$return["msg"]="您的余额不足，请到店内充值后继续";
		echo json_encode($return);
		exit();
	}
	$integral = floor($allprice*0.2);
	$intro = "购买";
	foreach($content as $k => $v){
		if($v['id']){
			$intro .= $v['name'].'x'.$v['num'].'=￥'.$v['price']*$v['num'].',';
			$goods_intro = '购买'.$v['name'].'x'.$v['num'].'=￥'.$v['price']*$v['num'];
			$goods_files = array(
					"userid"=>$mxuserid,
					"goodsid"    =>$v['id'],
					"intro"    =>$goods_intro,
					"adddate"  =>date('Y-m-d H:i:s')
			);
			$db->exe_insert("mx_app_user_goods_log",$goods_files);
		}
	}
	$intro = trim($intro,',');
	$files=array(
			"userid"=>$mxuserid,
			"number"    =>$allprice,
			"type"      =>2,
			"intro"    =>$intro,
			"createtime"  =>time(),
	);
	$db->exe_insert("mx_app_user_money",$files);
	$db->exe_update('mx_app_user', array('money'=>$mxuserInfo['money']-$allprice),"id=".$mxuserid);
	addIntegral($integral, $mxuserid,$intro);
	$targetuserid = $_COOKIE['share_targetuserid'];
	if($targetuserid){
		$newIntegral = $integral/2;
		$intro = "通过分享，".$mxuserInfo['realname'].$intro;
		giveIntegral($targetuserid, $newIntegral, $intro,2);
	}
	$return["success"]=true;
	$return["msg"]="结算成功，赠送您".$integral."积分！";
	if($return['success']){
		$cart->destroy();
	}
	echo json_encode($return);
	exit();
}
?>
<!DOCTYPE html>
<!-- saved from url=(0112)http://30581.vshop.weimob.com/cart.html?wechatid=onZOIt4IPzo9Gk5ltxqyUjOeobdw&v=d69b2f76c03115fbce151c15d22b8174 -->
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车</title>
<meta charset="UTF-8">
<meta name="viewport" content=" initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<link href="/css/style1.css" rel="stylesheet">
<script type="text/javascript" src="/js/jquery.1.7min.js"></script>
</head>
<body>
    
<div class="lay_header" style="height: 45px">
    <div class="lay_toptab mod_tab" id="lay_head">
         <a class="tab_item" href="/"><i class="qb_icon" style="background-image:url('/images/home_ico.png'); height: 22px;width: 22px;"></i></a>
        <a class="tab_item tab_item_logo" href="javascript:void(0);"><img src="images/20131212145408_6316.jpg"></a>
        <a class="tab_item" href="/user/index.php"><i class="qb_icon icon_icenter"></i></a>
        <a class="tab_item" href="/gouwuche.php"><i class="qb_icon icon_cart"></i><i class="qb_icon icon_number_bubble qb_none"></i></a>
    </div>
    <div class="lay_toptab mod_tab fixed qb_none" id="lay_head_fixed">
        <a class="tab_item" href="#"><i class="qb_icon icon_goback"></i></a>
        <a class="tab_item tab_item_logo" href="#"><img src="images/20131212145408_6316.jpg" style="width: 132px; margin-top: 8px;"></a>
        <a class="tab_item" href="#"><i class="qb_icon icon_icenter"></i></a>
        <a class="tab_item" href="#"><i class="qb_icon icon_cart"></i><i class="qb_icon icon_number_bubble qb_none"></i></a>
    </div>
</div>
<?php 
$list = $cart->contents();
if($list['total_items']){
?>
    <div class="lay_page current">
        <div class="lay_page_wrap">
            <input type="hidden" id="items-len" value="1">
            <input type="hidden" id="problem-Len" value="0">
            <section id="content">  
                <section id="item-sec">
                <?php foreach($list as $k => $v){
					if($v['id']){
				?>
                            <div class="mod_cell ui_plr0 ui_mt15 qb_mb15 item" id="0" last-count="1" price="1800">
                                <div class="mod_celltitle qb_fs_s ui_plr10">
                                    <h3 class="qb_fl"><i price="1800" class="qb_icon icon_checkbox icon_checkbox_checked" index="0" name="cart_checkbox"></i></h3>
                                </div>
                                <div class="ui_plr10">
                                    <ul class="mod_list mod_list_hr qb_fs_s">
                                        <li class="list_item qb_mb10 qb_bfc">
                                            <a href="articleshow.php?id=<?php echo $resdb["aid"]?>&fromwxopenid=<?php echo $wxmxopenid;?>" class="bfc_f">
                                                <img src="<?php echo $v['picurl'];?>"></a>
                                            <div class="bfc_c">
                                                <p><?php echo $v['name']?></p>
                                                <p>单价：<span class="ui_color_strong">￥<?php echo $v['price']?>元</span></p>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="qb_tar ui_p10 qb_fs_s ui_bg_color_weak ui-bt">
                                    <i class="qb_icon icon_trash ui_mr30 icon_delete" onclick="if(confirm('确定要删除该商品吗?')){window.location.href='gouwuche.php?actions=del&id=<?php echo $k;?>';};"></i>数量：<input type="tel" class="ui_textinput ui_textinput_tiny count" disabled value="<?php echo $v['num']?>">
                                    <span class="qb_tof" style="width: 100px">共计：<span class="ui_color_strong single-total">￥<?php echo $v['num']*$v['price']?>元</span></span>
                                </div>
                                <input type="hidden" class="cmdy-data" supportcod="true" num="1" limit="0" storenum="78" ic="26421" mid="72030" sa="">
                            </div>
                    <?php }}?>
                        
                    <div class="mod_cell ui_mt15 qb_mb15 ui_pt10">
                       
                        <div class="ui_pb10 ui_clearfix qb_fs_s">
                            <div class="qb_fl">
                                <span class="ui_mr30 ui_pt10">共<font color="green"><?php echo $list['total_items']?></font>种商品</span>
                                总价：<span class="ui_color_strong">￥<span id="total"><?php echo $list['cart_total']?></span></span>
                            </div>
                            <span class="mod_btn btn_strong qb_fr confirm_order" onclick="settle();">去结算</span>
                        </div>
                    </div>
                </section> 
            </section>
        </div>
    </div>
   <?php }else{?>
   <section id="content">  
                <section id="item-sec">
                    <div class="mod_cell ui_mt15 qb_mb15 ui_pt10">
                        <div class="ui_pb10 ui_clearfix qb_fs_s">
                            <div style="text-align:center;">
							您的购物车是空的。
                            </div>
                        </div>
                    </div>
                </section> 
            </section>
   	
   <?php }?>
   <script type="text/javascript">
	function settle(){
		var total = $('#total').html();
		var str = "您将消费 ￥"+total+" 确定去结算吗？";
		if(confirm(str)){
			$.post('#',{'actions':'settle'},function(data){
				if (data.success == true) {
					alert(data.msg);
					setTimeout('window.location.href=location.href',1000);
					return;
				} else {
					alert(data.msg);
				}
			},'json');
		}
	}
   </script>
<div class="lay_footer qb_tac qb_fs_s mod_mb15">
    
    <div class="fn_copyright qb_bfc">
        <div class="mod_color_comment bfc_c">©&nbsp;2013 铭讯版权所有</div>
    </div>
</div>
</body></html>